/* manySDSs.c
 *
 * Build this program with h4cc and run it to generate sds_empty_many.hdf,
 * which should be copied to hdp's testfiles directory.
 *
 * If you change anything in this file, you may need to update the
 * dumpsds-16.out and dumpsds-17.out test files in this directory.
 */

#include <stdlib.h>
#include <string.h>

#include "mfhdf.h"

#define X_LENGTH  10
#define Y_LENGTH  10
#define NUM_DSETS 40

int
main(void)
{
    int32       sd_id;
    int32       istat;
    int32       sds_list[NUM_DSETS];
    int32       dims[2];
    int32       rank;
    int32       len         = 0;
    const char *TEST_STRING = "This file was generated by the program manySDSs.c in hdf4/mfhdf/hdp/testfiles "
                              "to test the flag -k in hdp dumpsds";
    char        names[NUM_DSETS][8] = {
        {"data1"},  {"data2"},  {"data3"},  {"data4"},  {"data5"},  {"data6"},  {"data7"},  {"data8"},
        {"data9"},  {"data10"}, {"data11"}, {"data12"}, {"data13"}, {"data14"}, {"data15"}, {"data16"},
        {"data17"}, {"data18"}, {"data19"}, {"data20"}, {"data21"}, {"data22"}, {"data23"}, {"data24"},
        {"data25"}, {"data26"}, {"data27"}, {"data28"}, {"data29"}, {"data30"}, {"data31"}, {"data32"},
        {"data33"}, {"data34"}, {"data35"}, {"data36"}, {"data37"}, {"data38"}, {"data39"}, {"data40"}};

    /* Create and open the file and initiate the SD interface. */
    sd_id = SDstart("sds_empty_many.hdf", DFACC_CREATE);
    if (sd_id == FAIL) {
        fprintf(stderr, "manySDSs: SDstart returns sd_id: %d\n", sd_id);
        exit(EXIT_FAILURE);
    }

    /* Add file attribute to note about the file's history */
    len   = (int32)strlen(TEST_STRING) + 1;
    istat = SDsetattr(sd_id, "file_contents", DFNT_CHAR8, len, (void *)TEST_STRING);
    if (istat == FAIL) {
        fprintf(stderr, "manySDSs: SDsetattr returns status: %d\n", istat);
        exit(EXIT_FAILURE);
    }

    /* Define the rank and dimensions of the data set to be created. */
    rank    = 2;
    dims[0] = SD_UNLIMITED;
    dims[1] = X_LENGTH;

    /* Define the location, pattern, and size of the data set */
    for (int i = 0; i < NUM_DSETS; i++) {
        /* Create each data set. */
        sds_list[i] = SDcreate(sd_id, names[i], DFNT_INT16, rank, dims);
        if (sds_list[i] == FAIL) {
            fprintf(stderr, "manySDSs: SDcreate returns sds_list[%d]: %d\n", i, sds_list[i]);
            exit(EXIT_FAILURE);
        }
        /* Terminate access to each data set. */
        istat = SDendaccess(sds_list[i]);
        if (istat == FAIL) {
            fprintf(stderr, "manySDSs: SDendaccess returns status: %d\n", istat);
            exit(EXIT_FAILURE);
        }
    }

    /* Terminate access to the SD interface and close the file. */
    istat = SDend(sd_id);
    if (istat == FAIL) {
        fprintf(stderr, "manySDSs: SDend returns: %d\n", istat);
        exit(EXIT_FAILURE);
    }

    return EXIT_SUCCESS;
}
